 import lottie from 'lottie-web'
 import getfile  from './getfile.js'
 export default {
	 data() {
		return {
			player: null,
			pdata:{}
		}
	 },
	 methods: {
		 async render(val,oldValue,vm) {
			 let data,player;
			 if(val){
				data =JSON.parse(val)
				this.pdata=data
			 }else{
				data=this.pdata
				player=this.player
			 }
			 // console.log(data);
			 player = lottie.loadAnimation({
				container:document.getElementById(data.myCanvasId), // the dom element
				renderer: data.renderer,
				loop: data.loop,
				autoplay: data.autoPlay,
				// 动画json的本地数据
				animationData: data.data, 
				// 动画json的网络路径
				path: data.src?await getfile(data.src):''
			 })
			 
			 player.onComplete=(val)=>{ //动画播放完成触发
				//console.log('动画播放完成触发',val);
				vm.callMethod('receiveRenderData',{name:'onComplete',val})
			 }
			 player.onLoopComplete=(val)=>{ //当前循环播放完成触发
				//console.log('当前循环播放完成触发',val);
				vm.callMethod('receiveRenderData',{name:'onLoopComplete',val})
			 }
			 if(data.isOnChange){
				player.onEnterFrame=(val)=>{ //播放动画的时候触发
					//console.log('播放动画的时候触发',val);
					vm.callMethod('receiveRenderData',{name:'onEnterFrame',val})
				} 
			 }
			 player.onSegmentStart=(val)=>{ //开始播放一个动画片段的时候触发
				//console.log('开始播放一个动画片段的时候触发',val);
				vm.callMethod('receiveRenderData',{name:'onSegmentStart',val})
			 }
			 this.player=player
		 },
		 async callPlayer(val){
			if(!val.name)return;
			let {name, args} = val
			// console.log(name, args);
			if(Array.isArray(args)){
				this.player[name](...args)
			}else{
				this.player[name](args)
			}
		}
		 
	 }
 }